<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>
    <div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="ruleForm.pinyin"></el-input>
        </el-form-item>
        <el-form-item label="logo" prop="logo">
          <el-input v-model="ruleForm.logo"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label=关键字列表 prop="keywords">
          <el-input v-model="ruleForm.keywords"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="enable">
          <el-switch
              v-model="ruleForm.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        pinyin: '',
        description: '',
        sort: '',
        logo: '',
        keywords: '',
        enable: ''
      },
      // 表单验证规则
      rules: {
        name: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur'}  // 长度限制为4到15个字符的验证规则，当失去焦点时触发
        ],
        // 密码验证规则
        description: [
          {required: true, message: '请输入品牌简介', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur'}  // 长度限制为4到15个字符的验证规则，当失去焦点时触发
        ],
        // 排序验证规则
        sort: [
          {required: true, message: '请输入品牌排序序号', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '相册排序序号必须是0-99之间的数字', trigger: 'blur'}  // 长度限制为4到15
        ],
        pinyin: [
          {required: true, message: '请输入品牌的拼音', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {pattern: /^[a-z]+$/i, message: '相册排序序号必须是0-99之间的数字', trigger: 'blur'}  // 长度限制为4到15
        ],
        logo: [
          {required: true, message: '请输入品牌logo的URL', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {
            pattern: /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i,
            message: '长度在 2 到 35 个字符',
            trigger: 'blur'
          }  // 长度限制为4到15个字符的验证规则，当失去焦点时触发
        ],
        keywords: [
          {required: true, message: '请输入类别关键词,多个请使用","隔开', trigger: 'blur'},  // 必填项验证规则，当失去焦点时触发
          {min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur'}  // 长度限制为4到15个字符的验证规则，当失去焦点时触发
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      // 执行全部的检查规则
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('添加成功!');
          // 链接后端服务器的接口
          let url = 'http://localhost:9080/brand/add-new';
          // 打印url变量的数值
          console.log("url=" + url);
          // 使用axios发送POST请求到url指定的地址
          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData)
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 2000) {
              this.$message({
                message: '添加相册成功!',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style>

</style>